package com.sl.mes.app.controller.puboid;


import com.fasterxml.jackson.databind.JsonNode;
import com.sl.mes.app.controller.ApiRequestTools;
import com.sl.mes.app.service.puboid.PubOidService;
import com.sl.mes.app.util.JsonHandler;
import com.sl.mes.core.util.JacksonUtil;
import com.sl.mes.core.util.ResponseUtil;
import com.sl.mes.db.vo.PubOid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mes/puboid")
@Validated
public class PubOidController {

    @Resource
    private PubOidService pubOidService;

    /**
     * 根据主键进行查询
     *
     * @param body
     * @param request
     * @return
     */
    @PostMapping("getPubOidVoById")
    public Object getPubOidVoById(@RequestBody String body, HttpServletRequest request) {
        String group = JacksonUtil.parseString(body, "group");
        Map<Object, Object> result = new HashMap<Object, Object>();
        if (group.equals("")){
            return ResponseUtil.fail();
        }else if (pubOidService.getPubOidById(group)==null){
            return ResponseUtil.fail(0,"您输入的id不正确");
        } else {
            PubOid pubOid = pubOidService.getPubOidById(group);
            result.put("puboid", pubOid);
            return ResponseUtil.ok(result);
        }
    }

    /**
     * 根据主键进行查询
     *
     * @param body
     * @param request
     * @return
     */
    @PostMapping("getPubOidById")
    public String getPubOidById(@RequestBody String body, HttpServletRequest request) {
        String group = body.split("=")[1];
        Map<Object, Object> result = new HashMap<Object, Object>();
        PubOid pubOid = pubOidService.getPubOidById(group);
        return pubOid.getPubOid();
    }

    /**
     * 删除根据主键
     *
     * @param body
     * @param request
     */
    @PostMapping("deletePubOidVoById")
    public int deletePubOidVoById(@RequestBody String body, HttpServletRequest request) {
        String group = JacksonUtil.parseString(body, "group");
        if (group.equals("")){
            //输入错误信息
            return -1;
        }else if (pubOidService.getPubOidById(group)==null){
            //输入的id不存在
            return 0;
        }else {
            //正确返回删除的行数
            int count = pubOidService.deletePubOidById(group);
            return count;
        }
    }

    /**
     * 添加根据主键
     *
     * @param body
     * @param request
     */
    @PostMapping("insertPubOidVoById")
    public PubOid insertPubOidVoById(@RequestBody String body, HttpServletRequest request) {
        JsonNode movo = (JsonNode) JacksonUtil.toNode(body);
        JsonNode mo = (JsonNode) ((JsonNode) ((JsonNode) movo.get("data")).get("puboid"));
        PubOid pubOid = JsonHandler.fromJson(mo.toString(), PubOid.class);
        Map<Object, Object> result = new HashMap<Object, Object>();
        PubOid pubOidvo = pubOidService.insertPubOidById(pubOid);
        return pubOidvo;
    }

    /**
     * 修改
     *
     * @param body
     * @param request
     */
    @PostMapping("updataPubOidVoById")
    public Object updataPubOidVoById(@RequestBody String body, HttpServletRequest request) {
        PubOid pubOid = new PubOid();
        String[] params = body.split("&");
        for (int i = 0; i < params.length; i++) {
            String[] para = params[i].split("=");
            if (para[0].equals("groupcode")) {
                pubOid.setGroup(para[1]);
            } else if (para[0].equals("puboid")) {
                pubOid.setPubOid(para[1]);
            }
        }
        Map<Object, Object> result = new HashMap<Object, Object>();
        int count = pubOidService.updataPubOidVoById(pubOid);
        return count;
    }
}
